package ceura.ia.practica.android.ui;

import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import ceura.ia.practica.android.R;
import ceura.ia.practica.android.brush.Brush;
import ceura.ia.practica.android.listeners.ButtonListener;

/**
 * Clase que representa la vista de la barra superior de opciones.
 *
 * @author 66785403 (Jorge Madrid Portillo)
 */
public class BarraSuperior {

    private LaberintoView parent;
    private Context context;
    private LinearLayout barraSuperiorView;
    private ImageButton waterButton, grassButton, startButton, endButton, beginButton;

    /**
     *
     * @param context El contexto de Android.
     * @param parent El padre de esta vista.
     */
    public BarraSuperior(Context context, LaberintoView parent) {
        this.parent = parent;
        this.context = context;
        barraSuperiorView = (LinearLayout) parent.findViewById(R.id.barraSuperior);

        initChildren();

        initListeners();
    }

    private void initChildren() {
        waterButton = (ImageButton) barraSuperiorView.findViewById(R.id.btnAgua);
        grassButton = (ImageButton) barraSuperiorView.findViewById(R.id.btnBorrar);
        startButton = (ImageButton) barraSuperiorView.findViewById(R.id.btnInicio);
        endButton = (ImageButton) barraSuperiorView.findViewById(R.id.btnFin);
        beginButton = (ImageButton) barraSuperiorView.findViewById(R.id.btnEmpezar);
    }

    private void initListeners() {
        waterButton.setOnClickListener(new ButtonListener(Brush.WATER, parent));
        grassButton.setOnClickListener(new ButtonListener(Brush.GRASS, parent));
        startButton.setOnClickListener(new ButtonListener(Brush.START, parent));
        endButton.setOnClickListener(new ButtonListener(Brush.END, parent));
        beginButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                parent.findPath();
            }
        });
    }

    /**
     * Esconde o visualiza esta barra de herramientas mediante una animación.
     */
    public void toggle() {
        if (barraSuperiorView.getVisibility() == View.INVISIBLE) {
            barraSuperiorView.setVisibility(View.VISIBLE);
            barraSuperiorView.setClickable(true);
            Animation anim = AnimationUtils.loadAnimation(
                    context, R.anim.slide_down);
            barraSuperiorView.startAnimation(anim);
        } else {
            Animation slideOutAnim = AnimationUtils.loadAnimation(
                    context, R.anim.slide_up);
            barraSuperiorView.startAnimation(slideOutAnim);
            barraSuperiorView.setClickable(false);
            barraSuperiorView.setVisibility(View.INVISIBLE);
            // new Handler().postDelayed(new EndAnimation(), 600);
        }
    }

    private class EndAnimation implements Runnable {

        public void run() {
            barraSuperiorView.setVisibility(View.GONE);
        }
    }
}
